var floor = '';
var door = '';
var speed = '';
var power = '';
var run_status = '';
var lift_status = '';
var yxcs = '';
var kmcs = '';
// 定义方法取到电梯的数据
function getHxzRun() {
	return "floor=" + floor + ";door=" + door + ";speed=" + speed + ";power=" + power + ";run_status=" + run_status +
		";lift_status=" + lift_status + ";yxcs=" + yxcs + ";kmcs=" + kmcs;
}
localStorage.removeItem('verifycode');
localStorage.removeItem('dthxzverifycodeid');
localStorage.removeItem('accesstoken');
localStorage.removeItem('appkey');
localStorage.removeItem('lng');
localStorage.removeItem('lat');
//var timer;
$(function() {
	mui.init();
	var req = {
		'dtcode': localStorage.getItem('dtcode'),
		'userid': localStorage.getItem('userid'),
		'token': localStorage.getItem('token')
	}

	mui.ready(function() {
		// 加载框
		mui.showLoading("正在加载..", ".warn-list");
		Request();
		Request2();
		// 上面的代码 就放在最上面
		var timer = window.setInterval(setinterval, 5000); // 启动定时器


		// 关闭监控详情
		mui('.full-close').on('tap', 'i', function() {
			$('.monitor_det').fadeOut();
		})

		// 电梯详情跳转
		mui('.lift-record').on('tap', '.mui-table>div:first-child', function() {
			// 故障记录
			mui.openWindow({
				url: 'Yearly-table-fault.html',
			})
		}).on('tap', '.mui-table>div:nth-child(2)', function() {
			// 维保记录
			mui.openWindow({
				url: 'Yearly-table-repair.html',
			})
		}).on('tap', '.mui-table>div:nth-child(3)', function() {
			// 年检报告
			mui.openWindow({
				url: 'Yearly-table-report.html',
			})
		})

		// 监控详情页面跳转
		mui(".monitor-video").on('tap', '.mon-hisvid', function() {
			// 历史视频
			mui.openWindow({
				url: 'Yearly-history-video.html',
				id: 'Yearly-history-video.html'
			})
		});

	});

	// ajax请求处理数据
	function Request() {
		var url = domain + "/dtwbgl/dtdesc.h5";
		$.ajax(url, {
			data: req,
			dataType: 'json', // 服务器返回json格式数据
			type: 'post', // HTTP请求类型
			timeout: 30000, // 超时时间
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			async: true, // true异步加载
			success: function(data) {
				if (data.code == 2001) {
					// 返回值处理
					var ref = data.data;
					console.log(ref);
					// 电梯id 先移除,后保存
					localStorage.removeItem('dtid');
					localStorage.setItem('dtid', ref.dtid);

					/* 电梯梯龄处理 */
					var theAge = ref.age;
					if (theAge <= 5) {
						$('.lift-years .mui-table>div').eq(3).addClass('active');
					} else if (theAge > 5 && theAge <= 7) {
						$('.lift-years .mui-table>div').eq(2).addClass('active');
					} else if (theAge > 7 && theAge <= 10) {
						$('.lift-years .mui-table>div').eq(1).addClass('active');
					} else {
						$('.lift-years .mui-table>div').eq(0).addClass('active');
					}

					var changSuo = ref.changsuo; // 所在场所
					$('.lift-place>div:first-child h3').html(changSuo);

					var dtType = ref.dttype; // 设备用途
					$('.lift-place>div:nth-child(2) h3').html(dtType);

					var ycDays = ref.ycdays; // 年检状态

					if (ycDays > 45) {
						// 通过 正常色
						$('.lift-place>div:nth-child(3) h3 b').addClass('mui-icon-checkmarkempty');
					} else if (ycDays >= 0 && ycDays <= 45) {
						// 通过 警告色
						$('.lift-place>div:nth-child(3) h3 b').addClass('mui-icon-checkmarkempty');
						$('.lift-place>div:nth-child(3) h3 b').css('background-color', '#E4C072');
					} else {
						// 未通过 严重色
						$('.lift-place>div:nth-child(3) h3 b').addClass('mui-icon-closeempty');
						$('.lift-place>div:nth-child(3) h3 b').css('background-color', '#f26283');
					}

					var Wbcq = ref.wbcq; // 维保能力
					if (Wbcq == 0) {
						// 正常
						$('.lift-place>div:nth-child(4) h3').removeClass('on');
						$('.lift-place>div:nth-child(4) h3').html('正常');
					} else if (Wbcq == 1) {
						// 超期
						$('.lift-place>div:nth-child(4) h3').addClass('on');
						$('.lift-place>div:nth-child(4) h3').html('超期');
					} else {
						// 异常
						$('.lift-place>div:nth-child(4) h3').addClass('on');
						$('.lift-place>div:nth-child(4) h3').html('异常');
					}

					// 维保人员
					var wbry = ref.ry; // 根据type 值排序
					var wbryPx = wbry.sort(compare('type'));
					var len = wbryPx.length;
					var ryhtml = '';
					for (var i = 0; i < len; i++) {
						if (wbryPx[i].name == '') {
							// 安全员姓名为空时
							wbryPx[i].name = '暂无';
							wbryPx[i].mobile = '';
						}
						var ryType = ''; // 维保员类型
						if (wbryPx[i].type == 1) {
							ryType = '安全维保员';
						} else {
							ryType = '维保人员';
						}
						ryhtml +=
							`
                            <div class="mui-col-xs-6">
                                <div class="lift-repair-con">
                                    <div>
                                        <i class="mui-pull-left"></i>
                                        <div class="mui-pull-left">
                                            <h3>${wbryPx[i].name}</h3>
                                            <p>${ryType}</p>
                                        </div>
                                    </div>
                                    <div>
                                        <a href="tel:${wbryPx[i].mobile}">
                                            <b class="mui-icon mui-icon-phone"></b>
                                        </a>
                                    </div>
                                </div>  
                            </div>                       
                            `;
					}
					$('.lift-repair').append(ryhtml);


					// 注册代码
					var zhucode = ref.dtzhuccode;
					$('.lift-res li:first-child span').html(zhucode);

					// 电梯内部编码
					var bm = ref.fsydwnbbm;
					$('.lift-res li:nth-child(2) span').html(bm);

					// 下次年检日期
					var nexttime = ref.nextyearcheck;
					$('.lift-res li:nth-child(3) span').html(nexttime);

					// 电梯维保单位
					var wbdw = ref.wbname;
					$('.lift-res li:nth-child(4) span').html(wbdw);

					// 电梯维保负责人
					var fzr = ref.wbfuzeer;
					$('.lift-res li:nth-child(5) span').html(fzr);

					// 电梯维保电话
					var wbdh = ref.wbfuzetel;
					$('.lift-res li:nth-child(6) span').html(wbdh);

					// 品牌
					var dtpai = ref.dtpingpai;
					$('.lift-res li:last-child span').html(dtpai);

					// 隐藏加载框
					mui.hideLoading(function() { // 隐藏后的回调函数
					});
				} else if (data.code == 2002) {
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../login.html'
						})
					});
					console.log(data);
				} else {
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../404.html'
						})
					});
				}
			},
			error: function(xhr, type, errorThrown) { // 异常处理；
				// console.log(type);
				mui.alert('登录异常，请您重新登录');
				mui.openWindow({
					url: '../../login.html'
				})
			}
		});
	}
	// ajax 请求 黑匣子ID
	function Request2() {
		var gzurl = gzmain + '/lift/hxz5.jsp?req=gethxzinfo&zcdm=' + req.dtcode;
		$.ajax(gzurl, {
			dataType: 'json', // 服务器返回json格式数据
			timeout: 20000, // 超时时间
			async: true, // true异步加载
			success: function(data) {
				if (data.code == 2001) {
					// 返回值处理
					var ref = data.data;
					console.log(ref);
					if (ref.length != 0) {
						var dthxzid = ref[0].dthxzid; // 黑匣子ID
						var verifycode = ref[0].verifycode; // 视频观看code
						var deviceserial = ref[0].deviceserial;
						var accesstoken = ref[0].accesstoken;
						var appkey = ref[0].appkey;
						var lng = ref[0].lng;
						var lat = ref[0].lat;
						localStorage.setItem('lng', lng);
						localStorage.setItem('lat', lat);
						localStorage.setItem('dthxzid', dthxzid);
						localStorage.setItem('verifycode', verifycode);
						localStorage.setItem('deviceserial', deviceserial);
						localStorage.setItem('accesstoken', accesstoken);
						localStorage.setItem('appkey', appkey);
												
						if(verifycode.length==0 || deviceserial.length==0||accesstoken.length==0||appkey.length==0){
                        	 mui('.monitor-con').on('tap', 'a', function () {
                                 mui.alert('摄像头参数不完整，跳转在线演示', function () {
                                     mui.openWindow({
                                         url: 'showPage.html',
                                     })
                                 })
                             })
                        }else{
                        	 var vhref = 'dt:req=video;at=' + accesstoken + ';deviceSerial=' + deviceserial + ';verifyCode=' + verifycode + ';appkey=' + appkey + ';hxzid=' + dthxzid;
                             mui('.monitor-con').on('tap', 'a', function () {
                                 // 获取视频播放条件
                                 $('.monitor-con a').attr('href', vhref);
                                 window.location.href = this.href;
                             })
                        }
						
					} else {
						//clearInterval(timer);
						mui('.monitor-con').on('tap', 'a', function() {

							mui.alert('暂无摄像头，跳转在线演示', function() {
								mui.openWindow({
									url: 'showPage.html',
								})
							})
						})
					}

				} else if (data.code == 2002) {
					//clearInterval(timer);

					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../login.html'
						})
					});
				} else {
					//clearInterval(timer);

					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../404.html'
						})
					});
				}
			},
			error: function(xhr, type, errorThrown) { // 异常处理；
				// console.log(type);
				alert('登录异常，请您重新登录');
				mui.openWindow({
					url: '../../login.html'
				})
				console.log(data);
			}
		});
	}

	// 电梯数据定时器
	function setinterval() {
		var gzurl = gzmain + '/lift/hxz5.jsp?req=gethxzrun&hxzid=' + localStorage.getItem('dthxzid');
		$.ajax(gzurl, {
			dataType: 'json', // 服务器返回json格式数据
			timeout: 20000, // 超时时间
			async: true, // true异步加载
			success: function(data) {
				console.log('接口得到数据 ' + JSON.stringify(data));
				if (data.code == 2001) {
					// 返回值处理
					var ref = data.data;
					console.log("length " + ref.length);

					if (ref.length != 0) {
						floor = ref[0].floor;
						door = ref[0].door;
						speed = ref[0].speed;
						power = ref[0].power;
						run_status = ref[0].run_status;
						lift_status = ref[0].lift_status;
						kmcs = ref[0].kmcs;
						yxcs = ref[0].yxcs;
						console.log('getHxzRun得到的数据 ' + getHxzRun());
					} else {
						// $('.monitor_det').fadeOut();
						//clearInterval(timer);
						//console.log('黑匣子不存在或者不在线')
					}

				} else if (data.code == 2002) {
					// clearInterval(timer);
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../login.html'
						})
					});
				} else {
					//clearInterval(timer);
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../404.html'
						})
					});
				}
			},
			error: function(xhr, type, errorThrown) { // 异常处理；
				// clearInterval(timer);										// console.log(type);
				mui.openWindow({
					url: '../../404.html'
				})
			}
		});
	}

	// 视频播放需要数据
	function gethxzinfo() {
		var at = localStorage.getItem('accesstoken');
		var deviceSerial = localStorage.getItem('deviceserial');
		var verifyCode = localStorage.getItem('verifycode');
		var hxzid = localStorage.getItem('dthxzid');
		var appkey = localStorage.getItem('appkey');
		return "dt:req=video;at=" + at + ";deviceSerial=" + deviceSerial + ";verifyCode=" + verifyCode + ";appkey=" +appkey + ";hxzid=" + hxzid;
	}
	// 视频播放 缺少摄像头时 数据
	function errorhxzinfo() {
		var at = localStorage.getItem('accesstoken');
		var deviceSerial = localStorage.getItem('deviceserial');
		var verifyCode = localStorage.getItem('verifycode');
		var hxzid = localStorage.getItem('dthxzid');
		var appkey = localStorage.getItem('appkey');
		return "req=video;at=" + at + ";deviceSerial=" + deviceSerial + ";verifyCode=" + verifyCode + ";appkey=" + appkey +";hxzid=" + hxzid;
	}

	// 排序
	function compare(property) {
		return function(a, b) {
			var value1 = a[property];
			var value2 = b[property];
			return value1 - value2;
		}
	}

})
